/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package Servlet;

import client.MonitorClient;
import donnees.Bdd;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import server.Cpu;
import server.Memoire;
import server.Methode;
import server.Processus;

/**
 *
 * @author Emeline JUGE
 */
public class GestionMonitorServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        if (-1 != request.getRequestURI().indexOf("afficherHistorique")) {

            gererAffichageHistorique(request, response);
        } else if (-1 != request.getRequestURI().indexOf("afficherDetail")) {
            gererDetail(request, response);

        }
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        // gestion saisie IP
        if (-1 != request.getRequestURI().indexOf("monitorExecuterOperations")) {
            gererExecuterOperation(request, response);
        } else if (-1 != request.getRequestURI().indexOf("effacerHistorique")) {
            gererEffacerHistorique(request, response);

        }
    }

    private void gererExecuterOperation(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String[] checkboxOperation = request.getParameterValues("operation");
        String ip = request.getParameter("ip");
        StringBuilder sb = new StringBuilder();
        if (null != ip) {
            MonitorClient monMonitor = new MonitorClient();
            try {
                monMonitor.initialiser(ip);
                if (null != checkboxOperation) {

                    for (int i = 0; i < checkboxOperation.length; i++) {
                        String value = checkboxOperation[i];

                        // if null, it means checkbox is not in request, so unchecked 
                        if (value.equals("getListOfProcessus")) {
                            Processus proc = monMonitor.getProcessus();
                            if (null != proc) {
                                request.setAttribute("processus", proc.getProcess());
                            }

                        } else if (value.equals("getFreeMemory")) {
                            server.Memoire mem = monMonitor.getMemory();
                            if (null != mem) {
                                request.setAttribute("memoire", mem);
                            }
                        } else if (value.equals("getCPU")) {
                            Cpu cpu = monMonitor.getCpu();
                            if (null != cpu) {
                                request.setAttribute("cpu", cpu);
                            }

                        } else {
                            sb.append("<a href =\"monitor_operations.jsp\">Monitor</a></div>");
                        }
                    }
                    getServletContext().getRequestDispatcher("/affichage_operation.jsp").forward(request, response);
                } else {
                    sb.append("<h2>Aucune action selectionne !!</h2><div><a href =\"monitor_operations.jsp\">retour</a></div>");
                }
            } catch (Exception e) {
                Logger.getLogger(GestionMonitorServlet.class.getName()).log(Level.SEVERE, null, e);
                sb.append("<h2>Mauvaise IP !!</h2><div><a href =\"monitor_operations.jsp\">retour</a></div>");

            }
        } else {
            sb.append("<h2>Pas IP , merci de renseigner le champ!!</h2>");
        }
        PrintWriter out = response.getWriter();
        out.println(sb.toString());
    }

    /**
     * Gere l'affichage de l'historique.
     *
     * @param request la request
     * @param response la reponse
     * @throws IOException en cas d'execption
     */
    private void gererAffichageHistorique(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        Bdd accesBdd = new Bdd();
        List<Methode> listeMethdode = accesBdd.getListMethodesBDD();
        request.getSession().setAttribute("listeMethode", listeMethdode);
        getServletContext().getRequestDispatcher("/historique.jsp").forward(request, response);
    }

    /**
     * Gere l'affichage du detail de l'historique.
     *
     * @param request la request
     * @param response la reponse
     * @throws IOException en cas d'execption
     */
    private void gererDetail(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        Bdd accesBdd = new Bdd();
        String typeMehtode = request.getParameter("type");
        String id = request.getParameter("id");
        request.setAttribute("type", typeMehtode);

        if (typeMehtode.equals("getFreeMemory")) {
            Memoire mem = accesBdd.getMemoryBDD(Integer.parseInt(id));
            request.setAttribute("memoire", mem);
            getServletContext().getRequestDispatcher("/detailMemoire.jsp").forward(request, response);
        } else if (typeMehtode.equals("getCpu")) {
            server.Cpu cpu = accesBdd.getCpuBDD(Integer.parseInt(id));
            request.setAttribute("cpu", cpu);
            getServletContext().getRequestDispatcher("/detailCpu.jsp").forward(request, response);
        } else if (typeMehtode.equals("getTaskList")) {
            server.Processus process = accesBdd.getProcessusBDD(Integer.parseInt(id));
            request.setAttribute("tache", process.getProcess());
            System.out.println(process.getProcess().size());
            getServletContext().getRequestDispatcher("/detailTache.jsp").forward(request, response);
        }

    }

    /**
     * Gere la suppression de l'historique
     *
     * @param request la request
     * @param response la reponse
     * @throws IOException en cas d'execption
     */
    private void gererEffacerHistorique(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        Bdd accesBdd = new Bdd();
        accesBdd.deleteHistory();

        getServletContext().getRequestDispatcher("/effacerOK.jsp").forward(request, response);
    }

}
